#include "PreComplie.h"
#include "D3D.h"

namespace D3D
{
	IDirect3DDevice9* g_pDevice;

	bool Initialize(HWND wnd, int weight, int height, bool bFull)
	{
		IDirect3D9* pD3D = Direct3DCreate9(D3D_SDK_VERSION);
		D3DCAPS9 caps;
		pD3D->GetDeviceCaps(D3DADAPTER_DEFAULT, D3DDEVTYPE_HAL, &caps);
		int vp = 0;
		if(caps.DevCaps & D3DDEVCAPS_HWTRANSFORMANDLIGHT)
			vp = D3DCREATE_HARDWARE_VERTEXPROCESSING;
		else
			vp = D3DCREATE_SOFTWARE_VERTEXPROCESSING;

		D3DPRESENT_PARAMETERS params;
		params.BackBufferWidth = weight;
		params.BackBufferHeight = height;
		params.BackBufferFormat = D3DFMT_A8R8G8B8;
		params.BackBufferCount = 1;
		params.MultiSampleType = D3DMULTISAMPLE_NONE;
		params.MultiSampleQuality = 0;
		params.SwapEffect = D3DSWAPEFFECT_DISCARD;
		params.hDeviceWindow = wnd;
		params.Windowed = !bFull;
		params.EnableAutoDepthStencil = true;
		params.AutoDepthStencilFormat = D3DFMT_D16;
		params.Flags = 0;
		params.FullScreen_RefreshRateInHz = D3DPRESENT_RATE_DEFAULT;
		params.PresentationInterval = D3DPRESENT_INTERVAL_IMMEDIATE;

		HRESULT res = pD3D->CreateDevice(D3DADAPTER_DEFAULT, D3DDEVTYPE_HAL, wnd, vp, &params, &g_pDevice);
		if(FAILED(res))
		{
			MessageBoxA(NULL, "Init d3d failed", "Error", MB_OK);
			exit(0);
		}
		pD3D->Release();
		return true;
	}
}
